package com.dex4d.ui.main.model;

import com.dex4d.core.CustomApplication;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

/**
 * 作者    LiQingyi
 * 时间    2018/11/14 17:28
 * 文件    dex4d-android
 * 描述
 */
public class RxDao {

    public static Observable<List<TokenContract>> getTokens() {
        return Observable.create(new Observable.OnSubscribe<List<TokenContract>>() {
            @Override
            public void call(Subscriber<? super List<TokenContract>> subscriber) {
                List<TokenContract> list = CustomApplication.getDb().tokenContractDao().getAll();
                if (list == null || list.isEmpty()) {
                    subscriber.onError(new Throwable("list contains no elements"));
                } else {
                    subscriber.onNext(list);
                    subscriber.onCompleted();
                }
            }
        });

    }

    public static Observable<TokenContract> getToken(final String symbol) {
        return Observable.create(new Observable.OnSubscribe<TokenContract>() {
            @Override
            public void call(Subscriber<? super TokenContract> subscriber) {
                TokenContract tokenContract = CustomApplication.getDb().tokenContractDao().getItem(symbol);
                if (tokenContract == null) {
                    subscriber.onError(new Throwable("element is null"));
                } else {
                    subscriber.onNext(tokenContract);
                    subscriber.onCompleted();
                }
            }
        });

    }

}
